package cn.edu.scujcc;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommodityService {
@Autowired
	private  CommodityRepository repo;
    public Commodity createCommodity(Commodity c) {
	Commodity result= null;
	result = repo.save(c);
	return result;
}
	public Commodity getCommodity(User user) {
		Commodity result =null ;
		 Optional<Commodity> op = repo.findById(user);
		 if (op.isPresent()) {
			 result = op.get();
		 }
		return result;
	}
	public List<Commodity> getAllCommodity(){
		List<Commodity> result = null;
		result = repo.findAll();
		return result;
	}
	public Commodity updateChannel(Commodity c) {
		Commodity result = null;
		result = getCommodity(c.getId());
		if(result!= null) {
		if (c.getName() != null) {
			result.setName(c.getName());
		}
		result = repo.save(result);
		}
		return result;
	}
	public boolean deleteCommodity(User id) {
		boolean result =true;
		try {
			repo.deleteById(id);
		}catch (Exception c){
			result = false;
		}
		return result;
	}
}
